package com.flight.trans.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.CountDownTimer;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.base.BaseDialog;
import com.flight.trans.bean.NfcBean;
import com.flight.trans.util.DensityUtils;


/**
 * @author : YanKing
 * @date : 2019/5/19  9:53
 * @Description : 确定取消弹框
 */
public class AlertNFCDialog extends BaseDialog {
    private LinearLayout mLayoutBg;
    private TextView mTvType ,mTvCode;//内容

    private static AlertNFCDialog alertDialog ;

    public static AlertNFCDialog getInstance(Context mContext) {
        if(alertDialog == null)
            alertDialog = new AlertNFCDialog(mContext);
        return new AlertNFCDialog(mContext);
    }

    private AlertNFCDialog(Context mContext) {
        this.mContext = mContext;
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();
        builder();
    }

    private AlertNFCDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_nfc_alert, null);

        // 获取自定义Dialog布局中的控件
        mLayoutBg = view.findViewById(R.id.mLayoutBg);
        mTvType = view.findViewById(R.id.mTvType);
        mTvCode = view.findViewById(R.id.mTvCode);

        // 定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);

        mTvType.setMovementMethod(ScrollingMovementMethod.getInstance());
        mTvCode.setMovementMethod(ScrollingMovementMethod.getInstance());

        // 调整dialog背景大小
        mLayoutBg.setLayoutParams(new FrameLayout.LayoutParams( DensityUtils.getScreenWidth((Activity) mContext) / 3, DensityUtils.getScreenHeight((Activity) mContext) / 4));

        //点击空白处可消失
        setCancelable(true);
        return this;
    }

    /**
     * 设置内容
     *
     * @return
     */
    public AlertNFCDialog setContent(NfcBean nfcBean) {
        mTvType.setText(nfcBean.getType());
        mTvCode.setText(nfcBean.getCode());
        return this;
    }

    /**
     * 设置是否点击空白处消失,false为不消失
     *
     * @param cancel
     * @return
     */
    public AlertNFCDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    @Override
    public void show() {
        super.show();

        new CountDownTimer(3000, 1000) {
            public void onTick(long millisUntilFinished) {
            }

            public void onFinish() {
                if(mDialog.isShowing())
                    mDialog.dismiss();
            }
        }.start();
    }
}
